package com.example.customviewtest.api;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @auther XuDan on 2020/7/27.
 */
class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int lastx = 0;
    int lasty = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getRawX();
        float y = event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = x - lastx;
                float dy = y - lasty;
                setTranslationX(getTranslationX() + dx);
                setTranslationY(getTranslationY() + dy);
                break;
            case MotionEvent.ACTION_UP:
                float translationX = getTranslationX();
                float translationY = getTranslationY();
                Log.i("xd--------", "translationX=" + translationX + "---translationY=" + translationY);
                Log.i("xd--------", "left=" + getLeft() + "---top=" + getTop());
                Log.i("xd--------", "移动后的 left=" + (getLeft() + translationX) + "===top" + (getTop() + translationY));
                break;
        }
        lastx = (int) x;
        lasty = (int) y;
        return true;
    }
}
